<template>
<div>
	<content_top title="科目列表">
		
			<Button @click="$router.back()" slot="right" v-if="id>0"><Icon type="ios-arrow-back" size="16" style="margin-right:5px;" />返回</Button>
		
	</content_top>
	
	<div class="g_box">
		<div class="g_head">
			<h3>{{info?info.name:'数据列表'}}</h3>
			<div class="g_box_right">
				<ButtonGroup class="g_act_box">
					<Button @click="add">添加</Button>
				</ButtonGroup>
			</div>
		</div>
		<div class="g_box_con">
			<Table :loading="loading" :columns="Columns" :data="list" ref="table"></Table>
			<div style="font-size: 14px;overflow: hidden;padding: 10px;">
				<div style="float:left;">
					<Select v-model="batch_act" style="width:100px;;">
						<Option value="" >请选择</Option>
						<Option value="enable">显示</Option>
						<Option value="disconnected">隐藏</Option>
						<Option value="delete" >删除</Option>
					</Select>
					<Button @click="batch_action">确定</Button>
				</div>
				<div style="float:right;">
					<Page :total="count" show-elevator show-total @on-change="goto_page" />
				</div>
			</div>
		</div>
	</div>
<!-- 编辑币种 -->
	<info_box v-model="einfo.show" :cinfo="einfo.info" @success="success_info"></info_box>

</div>
</template>

<script>
import content_top from '@/themes/components/content_top';
import info_box from './info';

export default {
    name: 'subject_list',
    data () {
		let self = this;
        return {
			einfo:{
				show: false,
				info:{}
			},
            Columns: [
				{
					type: 'selection',
					width: 80,
					align: 'center'
				},
				{
					title: '科目代码',
					key: 'code',
					align: 'center',
                    width: 100,
                },
				{
                    title: '分类名称',
					key: 'name',
					width: 280,
                },
				{
                    title: '级别',
					key: 'leven',
					width: 120,
					align: 'center',
                },
				{
                    title: '科目类别',
					key: 'type_name',
					width: 140,
					align: 'center'
				},
				{
                    title: '是否显示',
					key: 'is_enable',
					width: 120,
					render: (h, params) => {
						return h('i-switch',{
							props:{
								'true-value':1,
								'false-value':0,
								value:parseInt(params.row.is_enable)
							},
							on:{
								'on-change':function(val){
									
									self.set_enable(params.row.id,val);
								}
							}
						});
					}
                },
				{
					title:'设置',
					key: 'action',
					align: 'center',
					width: 200,
					render: (h, params) => {
						return h('div', [
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px',
								},
								on: {
									click: () => {
										self.add({parent_id:params.row.id});
									}
								}
							}, '新增下级'),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									
								},
								on: {
									click: () => {
										self.$router.push({name:'s_subject_child',params:{id:params.row.id}});
									}
								}
							}, '查看下级')
						]);
					}
				},
				{
					title:'操作',
					key: 'action',
					align: 'center',
					render: (h, params) => {
						return h('div', [
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									marginRight: '5px',
								},
								on: {
									click: () => {
										this.dbedit(params.row)
									}
								}
							}, '编辑'),
							h('Button', {
								props: {
									type: 'info',
									size: 'small',
								},
								style: {
									
								},
								on: {
									click: () => {
										self.remove(params.row.id)
									}
								}
							}, '删除')
						]);
					}
				}
            ],
			list:[],
			loading:false,
			filter:{
				
			},
			count:0,
			page:1,
			batch_act: '',
			id:0,
			info:null
        };
	},
	components:{
		content_top,
		info_box,
	},
	created(){
		this.id = this.$route.params.id;
			
		this.load_data();
		if(this.id){
			this.get_info();
		}
	},
	watch:{
		$route(val){
			this.id = val.params.id;
			this.info = null;
			this.page = 1;
			this.list = [];
			this.load_data();
			if(this.id){
				this.get_info();
			}
		}
	},
	methods:{
		get_info(){
			var self = this;
			
			var postdata = {
				id:this.id
			}
			
			Whttp.load('User/Subject/get_info',postdata,function(res){
				self.info = res;
			});
		},
		load_data(){
			var self = this;
			
			
			var postdata = {
				page:this.page
			}

			if(this.id>0){
				this.einfo.info.parent_id = this.id;
				postdata['parent_id'] = this.id;
			}
			
			
			Whttp.load('User/Subject/get_list',postdata,function(res){
				self.list = res.list;
				self.count = parseInt(res.count);
			});
		},
		goto_page(page){
			this.page = page;
			this.load_data();
		},
		dbedit(row){
			this.einfo.info = row;
			this.einfo.show = true;
		},
		add(data){
			var data = data || {};
			this.einfo.info = data;
			this.einfo.show = true;
		},
		remove(id){
			var self = this;
			if(!confirm('是否删除')){
				return false;
			}
			var postdata = {
				id:id
			};
			Whttp.load('User/Subject/del',postdata,function(result){
				self.load_data();
				self.$Message.info('删除成功');
			});
		},
		batch_action(){
			var ids = [];
			if(this.batch_act==''){
				alert('请选择操作');
				return;
			}
			if(this.batch_act=='delete'){
				if(!confirm('是否批量删除该科目')){
					return false;
				}
			}
			
			var sels = this.$refs['table'].getSelection();
			
			for(var i=0;i<sels.length;i++){
				ids.push(sels[i].id);
			}
			
			var self = this;
			var postdata = {
				ids:ids,
				act:this.batch_act
			};
			Whttp.load('User/Subject/batch_act',postdata,function(result){
				self.load_data();
				self.$Message.info('操作成功');
				self.batch_act = '';
			});
			
			
		},
		success_info(res){
			this.load_data();
		},
		set_enable(id,status){
			var self = this;
			var postdata = {};
			
			postdata = {
				status:status,
				id:id
			};
			
			Whttp.load('User/Subject/set_enable',postdata,function(res){
				self.$Message.info('设置成功');
			});
		},
	}
};
</script>
<style scoped>
.price{
	color: red;
    margin: 0 5px;
}
</style>